package com.gpframework.module.controller.process;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Arrays;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.exception.ParamException;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.manufacture.domain.GpProduceProduct;
import com.gpframework.module.manufacture.domain.req.GpProductionDetailsUpdate;
import com.gpframework.module.manufacture.service.IGpProduceProductService;
import com.gpframework.module.order.domain.GpProduct;
import com.gpframework.module.order.service.IGpProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.module.process.domain.GpProcessScheduling;
import com.gpframework.module.process.domain.vo.GpProcessSchedulingVo;
import com.gpframework.module.process.domain.req.GpProcessSchedulingReq;
import com.gpframework.module.process.domain.req.GpProcessSchedulingAdd;
import com.gpframework.module.process.domain.req.GpProcessSchedulingUpdate;
import com.gpframework.module.process.service.IGpProcessSchedulingService;
import com.gpframework.common.utils.poi.ExcelUtil;
import javax.validation.Valid;
import io.swagger.annotations.*;
import com.gpframework.common.core.page.TableDataInfo;
/**
 * @ClassName GpProcessSchedulingController
 * @Author gpframework
 * @Date 2023-12-20
 * @Version 1.0.0
 * @Description 排单Controller 接口类
 */
@Validated
@Api(tags = "排单")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/process/scheduling" )
public class GpProcessSchedulingController extends BaseController {

    private final IGpProcessSchedulingService iGpProcessSchedulingService;



 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2023-12-20
  * @Param gpProcessScheduling
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @GetMapping("/list")
    public TableDataInfo<GpProcessSchedulingVo> list(GpProcessSchedulingReq gpProcessSchedulingReq)
    {
        startPage();
        QueryWrapper<GpProcessScheduling> lqw = new QueryWrapper<GpProcessScheduling>();
        gpProcessSchedulingReq.generatorQuery(lqw,true);
        List<GpProcessSchedulingVo> list = iGpProcessSchedulingService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessScheduling
     * @Return AjaxResult
     */
    @ApiOperation("详情")
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpProcessSchedulingVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpProcessSchedulingService.getVoById(id));
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessScheduling
     * @Return AjaxResult
     */
    @ApiOperation("新增")
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpProcessSchedulingAdd gpProcessSchedulingAdd) {
        GpProcessScheduling gpProcessScheduling = Convert.convert(new TypeReference<GpProcessScheduling>() {}, gpProcessSchedulingAdd);
        return toAjax(iGpProcessSchedulingService.save(gpProcessScheduling) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessScheduling
     * @Return AjaxResult
     */
    @ApiOperation("修改")
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpProcessSchedulingUpdate gpProcessSchedulingUpdate) {
        return toAjax(iGpProcessSchedulingService.editOne(gpProcessSchedulingUpdate) ? 1 : 0);
    }


    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessScheduling
     * @Return AjaxResult
     */
    @ApiOperation("删除")
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpProcessSchedulingService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * @Description 生成收货单
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessScheduling
     * @Return AjaxResult
     */
    @ApiOperation("生成收货单")
    @Log(title = "生成收货单" , businessType = BusinessType.INSERT)
    @PostMapping("/createReceiver")
    public AjaxResult createReceiver(@Valid @RequestBody GpProcessSchedulingUpdate gpProcessSchedulingUpdate) {
        if (gpProcessSchedulingUpdate == null || CollectionUtils.isEmpty(gpProcessSchedulingUpdate.getIds())) {
            return AjaxResult.parameterError();
        }
        return toAjax(iGpProcessSchedulingService.createReceiveGoods(gpProcessSchedulingUpdate.getIds(), gpProcessSchedulingUpdate.getCreateBy()) ? 1 : 0);
    }


    /**
     * @Description 确认下单
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessScheduling
     * @Return AjaxResult
     */
    @ApiOperation("确认下单")
    @Log(title = "确认下单" , businessType = BusinessType.INSERT)
    @PostMapping("/confirmOrder ")
    public AjaxResult confirmOrder(@Valid @RequestBody List<GpProcessSchedulingUpdate> gpProcessSchedulingUpdates) {
        for (GpProcessSchedulingUpdate gpProcessSchedulingUpdate : gpProcessSchedulingUpdates) {
            if (gpProcessSchedulingUpdate == null || CollectionUtils.isEmpty(gpProcessSchedulingUpdate.getIds())) {
                return AjaxResult.parameterError();
            }
        }
        return toAjax(iGpProcessSchedulingService.confirmOrder(gpProcessSchedulingUpdates) ? 1 : 0);

    }

}
